﻿using System;
using System.Drawing;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Threading;
using System.Windows;

namespace Vtron.LEDCorrective.Client
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Uri tcpAdrs = new Uri("net.tcp://localhost:8888/WPFHost/");
            Uri httpAdrs = new Uri("http://localhost:9999/WPFHost/");
            Uri[] baseAdresses = { tcpAdrs, httpAdrs };
           ServiceHost host = new ServiceHost(typeof(MessageService), baseAdresses);

            NetTcpBinding tcpBinding = new NetTcpBinding(SecurityMode.None, true);
            tcpBinding.MaxBufferPoolSize = (int)67108864;
            tcpBinding.MaxBufferSize = 67108864;
            tcpBinding.MaxReceivedMessageSize = (int)67108864;
            tcpBinding.TransferMode = TransferMode.Buffered;
            tcpBinding.ReaderQuotas.MaxArrayLength = 67108864;
            tcpBinding.ReaderQuotas.MaxBytesPerRead = 67108864;
            tcpBinding.ReaderQuotas.MaxStringContentLength = 67108864;
            tcpBinding.MaxConnections = 100;

            ServiceThrottlingBehavior throttle;
            throttle = host.Description.Behaviors.Find<ServiceThrottlingBehavior>();
            if (throttle == null)
            {
                throttle = new ServiceThrottlingBehavior();
                throttle.MaxConcurrentCalls = 100;
                throttle.MaxConcurrentSessions = 100;
                host.Description.Behaviors.Add(throttle);
            }
            ServiceMetadataBehavior metadata = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
            if (throttle != null)
            {
                metadata.HttpsGetEnabled = false;
            }

            tcpBinding.ReceiveTimeout = new TimeSpan(20, 0, 0);
            tcpBinding.ReliableSession.Enabled = true;
            tcpBinding.ReliableSession.InactivityTimeout = new TimeSpan(20, 0, 10);

            host.AddServiceEndpoint(typeof(IMessage), tcpBinding, "tcp");
            host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "net.tcp://localhost:10000/WPFHost/mex");

            try
            {
                host.Open();
            }
            catch (Exception ex)
            {
                System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                                                       new Action(() =>
                                                       {
                                                           MessageBox.Show("错误：" + ex.Message);
                                                       }));
            }
            finally
            {

            }
        }
    }


    [ServiceContract(CallbackContract = typeof(IMessageCallback), SessionMode = SessionMode.Required)]
    public interface IMessage
    {
        [OperationContract(IsInitiating = true)]
        void Show(Color color, int left, int up, int width, int height, int blockColumn, int blockRow, int blockNum);
    }

    public interface IMessageCallback
    {
        //[OperationContract(IsOneWay = true)]
        //void RefreshClients();
    }

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
    public class MessageService : IMessage
    {
        //public IMessageCallback CurrentCallback
        //{
        //    get
        //    {
        //        return OperationContext.Current.GetCallbackChannel<IMessageCallback>();
        //    }
        //}

        FullScreenWindow window = null;

        public void Show(Color color, int left, int up, int width, int height, int blockColumn, int blockRow, int blockNum)
        {
            System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                                                        new Action(() =>
                                                        {
                                                            FullScreenWindow windowNew = new FullScreenWindow(width, height, new Thickness(left, up, 0, 0), color, blockColumn, blockRow, blockNum);
                                                            windowNew.Show();

                                                            if (window != null)
                                                            {
                                                                window.Close();
                                                            }

                                                            window = windowNew;
                                                        }));
        }
    }
}
